package session

import (
	"myadmin/sys/cache/mapstore"
	"myadmin/sys/cache/redis"
	"myadmin/sys/tool"
	"strings"
	"time"
)

/*
redis 实现session
 */

type RedisSession struct{}

func NewRedisSession() *RedisSession{
	return new(RedisSession)
}

func(this *RedisSession)Get(k string) *Session{
	var ses = &Session{}
	redis.GetObj(strings.Join([]string{"user",k},":"),ses)
	return ses
}

func(this *RedisSession) Add(session *Session) string{
	uuid := tool.GetUUID()
	exp:=mapstore.Get("sessionexp").(int)
	redis.SetObj(strings.Join([]string{"user",uuid},":"),session,exp)
	return uuid
}

func(this *RedisSession) Update(k string, s *Session){
	exp:=mapstore.Get("sessionexp").(int)
	redis.SetObj(strings.Join([]string{"user",k},":"),s,exp)
}

func(this *RedisSession) UpdateTime(k string, s *Session){
	s.ActiveTime = time.Now().Unix()
	exp:=mapstore.Get("sessionexp").(int)
	redis.SetObj(strings.Join([]string{"user",k},":"),s,exp)

}
func (this *RedisSession) Del(k string){
	redis.Del(strings.Join([]string{"user",k},":"))
}